package edu.njfu.javawebdemo.dao;

import edu.njfu.javawebdemo.model.User;
import edu.njfu.javawebdemo.util.DBHelper;
import edu.njfu.javawebdemo.util.Md5Util;
import org.junit.jupiter.api.DynamicTest;
import sun.security.rsa.RSASignature;

import java.util.ArrayList;
import java.util.List;

public class UserDao {
//user对象的增删改查
   public boolean checkLogin(User user){
//sql statement
       String sql="select * from tb1_user where username=? and password=?";
//把占位符的值放到一个list中
       List<Object>params=new ArrayList<Object>();
       params.add(user.getUserName());
       //把明文转换成密文
       String  encryptPassword= Md5Util.stringToMD5(user.getPassword());
       params.add(encryptPassword);
       //创建DBHelper对象
       DBHelper dbHelper = new DBHelper();
       List<Object> query = dbHelper.query(sql, params);
     //  System.out .println(query);
       //Map对象

       return query.size()>0;
   }
   //创建saveUser(User user)
    public  boolean saveUser(User u){
String sql="insert into tb1_user(username,password) value(?,?)";
//对占位符进行赋值
        List<Object> params=new ArrayList<Object>();
        params.add(u.getUserName());
       params.add(Md5Util.stringToMD5(u.getPassword()));
        DBHelper db= new DBHelper();
        int  result = db.update(sql,params);
       return result>0;
    }
}
